package common.action;

import static common.utils.Constants.ADMIN_ID;
import static common.utils.Constants.LOGINNAME;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import service.impl.AdminServiceImpl;

import common.pojo.SysAdmin;
import common.utils.Constants;
import common.utils.MD5Util;
import common.utils.SessionUtils;

@Scope("prototype")
@Controller(value = "admin")
public class AdminAction extends BaseAction {

	/**
	 * 登录验证
	 * 
	 * @throws IOException
	 */
	public void login() throws IOException {
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		//获取管理员信息
		SysAdmin ad = adminService.getByName(name);
		if (ad == null) {
			response.getWriter().write("{\"success\":false,\"msg\":1}");
		} else if (!StringUtils.equals(MD5Util.md5(pwd), ad.getPassword())) {
			response.getWriter().write("{\"success\":false,\"msg\":2}");
		} else {
			response.getWriter().write("{\"success\":true}");
			session.setAttribute(ADMIN_ID, ad.getId());
			session.setAttribute(LOGINNAME, name);
			
			ad.setLastLoginTime(new Date());
			InetAddress addr = InetAddress.getLocalHost();
			String ip = addr.getHostAddress().toString();
			ad.setLastLoginIp(ip);
			adminService.update(ad);
		}
		
	}


	/**
	 * 修改密码
	 * 
	 * @return
	 * @throws IOException
	 */
	public String updatepwd() throws IOException {
		String oldPWD = request.getParameter("oldpwd");
		String newPWD = request.getParameter("newpwd");
		SysAdmin ad = adminService.findById((Integer) session
				.getAttribute(ADMIN_ID));
		if (MD5Util.md5(oldPWD).equalsIgnoreCase(ad.getPassword())) {
			ad.setPassword(MD5Util.md5(newPWD));
			ad.setUpdateName((String)session.getAttribute(Constants.LOGINNAME));
			ad.setUpdateTime(new Date());
			adminService.updatepwd(ad);
			return "success2";
		} else {
			return "exception";
		}
	}

	/**
	 * 根据id查找信息
	 * @return
	 */
	public String findById() {
		int id = Integer.valueOf(request.getParameter("id"));
		admin = adminService.findById(id);
		return "detail";
	}

	/**
	 * 保存管理员
	 * @return
	 */
	public String saveAdmin() {
		admin.setCreateName(SessionUtils.getAdminName(request));
		adminService.saveAdmin(admin);
		if (StringUtils.isBlank(dofunction))
			dofunction = "()";// 刷新页面
		return "success2";
	}

	/**
	 * 检查管理员用户名
	 * @throws Exception
	 */
	public void checkAddmin() throws Exception{
		String name=request.getParameter("loginName");
		admin=adminService.getByName(name);
		if(admin!=null){
			response.getWriter().write("{\"success\":false}");
		}else{
			response.getWriter().write("{\"success\":true}");
		}		
	}

	/**
	 * 删除session信息，做退出用
	 */
	public String destorySession() {
		session.invalidate();
		return "index";
	}

	private static final long serialVersionUID = 1L;

	//引用service层
	@Resource
	private AdminServiceImpl adminService;
//	@Autowired
//	private IHotelOrderService hotelOrderService;
//	@Resource
//	private IHotelReviewService reviewService;
	private int ordercount;
	private int reviewcount;
	private SysAdmin admin;
	private List<SysAdmin> adminlist;
	private String pagination;
	private String dofunction;

	public int getOrdercount() {
		return ordercount;
	}

	public void setOrdercount(int ordercount) {
		this.ordercount = ordercount;
	}

	public int getReviewcount() {
		return reviewcount;
	}

	public void setReviewcount(int reviewcount) {
		this.reviewcount = reviewcount;
	}



	public String getDofunction() {
		return dofunction;
	}

	public void setDofunction(String dofunction) {
		this.dofunction = dofunction;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

	public List<SysAdmin> getAdminlist() {
		return adminlist;
	}

	public void setAdminlist(List<SysAdmin> adminlist) {
		this.adminlist = adminlist;
	}

	public SysAdmin getAdmin() {
		return admin;
	}

	public void setAdmin(SysAdmin admin) {
		this.admin = admin;
	}

}
